using MarkdownSharp;
namespace Canlipe.Utils
{
	public class ColorCodeToMarkdownSharpBridge : IHighlighter
	{
		static ColorCode.CodeColorizer colorizer = new ColorCode.CodeColorizer ();

		public ColorCodeToMarkdownSharpBridge ()
		{
		}

		private ColorCode.ILanguage GetLanguage (string langCode)
		{
			// first line pattern return null for all langs in ColorCode, so... yeah...
			switch (langCode.ToLower ()) {
			case "::cpp":
				return ColorCode.Languages.Cpp;
			case "::csharp":
			case "::c#":
				return ColorCode.Languages.CSharp;
			case "::html":
				return ColorCode.Languages.Html;
			case "::java":
				return ColorCode.Languages.Java;
			case "::javascript":
			case "::js":
				return ColorCode.Languages.JavaScript;
			case "::php":
				return ColorCode.Languages.Php;
			case "::sql":
				return ColorCode.Languages.Sql;
			}

			return None.Instance;
		}

		#region IHighlighter implementation

		public string Highlight (string text)
		{
			string firstline = text.Substring (0, text.IndexOf ('\n')).Trim ();
			if (firstline.StartsWith ("::")) { // remove the language tag
				text = text.Substring (text.IndexOf ('\n')).Trim (new [] { '\r' }
				);
			}

			return colorizer.Colorize (text, this.GetLanguage (firstline));
		}

		#endregion
	}
}

